use Wx 0.15 qw[:allclasses];
use strict;
package wxSurvival;

use Wx qw[:everything];
use base qw(Wx::Panel);
use strict;
use Storable;
use wxNAPEntry;
use wxGNAPEntry;
use wxSurvivalEntry;
use Scalar::Util qw(blessed);


sub new {

	my $self = shift;
	#Create the panel
	$self = $self->SUPER::new( @_);


	#The entry windows
	$self->{nap} = wxNAPEntry->new($self, -1);
	$self->{gnap} = wxGNAPEntry->new($self, -1);

	#The combo box
	$self->{combo_title} = Wx::StaticText->new($self, -1, "Problem type:", wxDefaultPosition, wxDefaultSize);
	$self->{combo_problem} = Wx::ComboBox->new($self, -1, '', wxDefaultPosition, wxDefaultSize, ['PD','NAP','gNAP'],wxCB_READONLY);
	$self->{combo_problem}->SetValue('PD');
	
	#The status bar -- unused at the moment
	$self->{label_status} = Wx::StaticText->new($self, -1, "", wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
	
	my $pd_title = "Problem Types\n";
	my $pd_text = "Phylogenetic diversity is the simplest optimisation goal. In this case all species are considered equal - the goal is simply to find the species that have the highest PD score.";
	$pd_text .= "\n\nThe Noah's Ark Problem (NAP) allows each species to be assigned a survival probability which can be increased by spending some money on its conservation."; 
	$pd_text .= "\n\nThe generalised Noah's Ark Problem (GNAP) permits more complicated relationships between species survival and conservation expenditure to be specified.";

	#The PD info text
	$self->{pd} = Wx::Panel->new($self, -1);
	$self->{pd_title} =  Wx::StaticText->new($self->{pd}, -1, $pd_title, wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT|wxST_NO_AUTORESIZE|wxRAISED_BORDER);
	$self->{pd_text} = Wx::StaticText->new($self->{pd}, -1, $pd_text, wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT|wxST_NO_AUTORESIZE|wxRAISED_BORDER);
	
	#Set title font
	my $font = $self->{pd_title}->GetFont();
	$font->SetPointSize($font->GetPointSize()+2);
	$font->SetWeight(wxFONTWEIGHT_BOLD);
	$self->{pd_title}->SetFont($font);
	
	#Do the layout
	$self->{pd_sizer} = Wx::BoxSizer->new(wxVERTICAL);
	$self->{pd}->SetSizer($self->{pd_sizer});
	
	$self->{pd_text}->Wrap(400);
	$self->{pd_sizer}->AddStretchSpacer(1); 
	$self->{pd_sizer}->Add($self->{pd_title}, 0, wxALIGN_CENTER_HORIZONTAL);
	$self->{pd_sizer}->Add($self->{pd_text}, 5, wxALIGN_CENTER_HORIZONTAL);
	$self->{pd_sizer}->AddStretchSpacer(2); 

	$self->{sizer} = Wx::BoxSizer->new(wxVERTICAL);
	$self->{sizer_selection} = Wx::BoxSizer->new(wxHORIZONTAL);

	$self->{sizer_selection}->Add($self->{combo_title}, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
	$self->{sizer_selection}->Add($self->{combo_problem}, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
	$self->{sizer_selection}->Add($self->{label_status}, 1, wxEXPAND | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
	
	$self->{sizer}->Add($self->{sizer_selection}, 0, wxEXPAND );

	$self->SetSizer($self->{sizer});

	$self->problem_select();
	
	#Register events
	Wx::Event::EVT_COMBOBOX($self, $self->{combo_problem}->GetId, \&problem_select);
	return $self;
}

#Set the state
sub set_state {
	my $self = shift;
	$self->{state} = shift;
	$self->{nap}->set_state($self->{state});
	$self->{gnap}->set_state($self->{state});	
}

#Sets the appropriate interface to visible and sets the right survival object
sub problem_select {
	my $self = shift;
	my $selection = $self->{combo_problem}->GetValue();
	unless ($selection) {
		$self->{combo_problem}->SetValue('PD');
		return;
	}
	
	$self->{sizer}->Remove($self->{pd});
	$self->{sizer}->Remove($self->{nap});
	$self->{sizer}->Remove($self->{gnap});
	$self->{pd}->Hide();
	$self->{nap}->Hide();
	$self->{gnap}->Hide();
	
	my $toAdd;	
	
	if ($selection eq 'PD') {
		$toAdd = $self->{pd};
		$self->{state}->{survival} = $self->{state}->{survival_pd};		
	} elsif ($selection eq 'NAP') {
		$toAdd = $self->{nap};
		$self->{state}->{survival} = $self->{state}->{survival_nap};		
	} elsif ($selection eq 'gNAP') {
		$toAdd = $self->{gnap};
		$self->{state}->{survival} = $self->{state}->{survival_gnap};		
	}

	$toAdd->Show();
	
	$self->{sizer}->Add($toAdd, 1, wxEXPAND | wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL );
	$self->{sizer}->Fit($self);

	unless ($selection eq 'PD') { $toAdd->set_splitter_position(); }
	
	$self->Layout();	

#	delete $self->{state}->{solutions};
#	$self->GetParent()->GetParent()->send_update_signal();
}

sub update {
	my $self = shift;
	$self->{nap}->update();
	$self->{gnap}->update();
}

1;